<?php

namespace services\models;

use Yii;

/**
 * This is the model class for table "d_mini.t_file".
 *
 * @property integer $id
 * @property string $c_uri
 * @property integer $uid
 * @property string $db_time
 * @property integer $op_id
 * @property string $title
 * @property string $original
 * @property string $c_type
 * @property string $size
 */
class UpFileData extends \common\models\UpFileData
{


    /**
     * 根据图片id，批量返回图片地址
     * @param string $ids 1,2,3
     * @param int  $number 指定显示多少张图片
     */
    public  static function showImages($ids, $number=1) {

        $ids = trim(preg_replace('/,+/', ',', $ids), ',');
        if(!preg_match('/^[\d,]+$/', $ids)){
            return [];
        }
        
        $query = static::find()->where("id in ( {$ids} )");
        
        
        if($number!=0){
         $result = $query->limit($number)->all();
         }else{
         $result = $query->all();
         }
         $items = [];
         foreach($result as $item){
             $items[] = Yii::getAlias('@static').Yii::$app->params['upfile'].$item->c_uri;
         }
         return $items;
    }
    
    /**
     * 根据图片id返回当前图片的路径，
     * @param int $id
     * @param boolean 是否返回其他数据
     * @return string
     */
    public  static function showImage($id,$params=false) {
        if(!preg_match('/^[\d]+$/', $id)){
            return '';
        }
    
        $query = static::findOne($id);
        if(empty($query)){
            return '';
        }
        if($params) {
            return [
                'url' => Yii::getAlias('@static') . Yii::$app->params['upfile'] . $query->c_uri,
                'name' => $query->original,
                'title'=>$query->title
            ];
            
        } else {
            return Yii::getAlias('@static').Yii::$app->params['upfile'].$query->c_uri;
        }

    }

    public static function getFirstImage($ids){
        $ids = trim(preg_replace('/,+/', ',', $ids), ',');
        if(!preg_match('/^[\d,]+$/', $ids)){
            return '';
        }
        $ids = explode(',', $ids);
        sort($ids);
        return static::showImage(array_shift($ids));
    }
}
